<template>
  <div class="login">
    <div class="form el-col-10 el-row is-justify-center">
      <h2 class="title">办公室日常管理系统</h2>
      <el-form
        :model="ruleForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="80px"
        class="demo-ruleForm el-col-15"
        @keydown.enter="submitForm('ruleForm')"
      >
        <el-form-item
          label="用户名"
          prop="pass"
        >
          <el-input
            type="text"
            v-model="ruleForm.pass"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="密码"
          prop="checkPass"
        >
          <el-input
            type="password"
            v-model="ruleForm.checkPass"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            @click="submitForm('ruleForm')"
          >登录</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
        <p>还没有账号？点击<router-link to="/register"><b>注册</b></router-link></p>
      </el-form>
    </div>
  </div>

</template>

<script>
export default {
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入用户名"));
      } else {
        if (this.ruleForm.checkPass !== "") {
          this.$refs.ruleForm.validateField("checkPass");
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        pass: "",
        checkPass: "",
      },
      rules: {
        pass: [{ validator: validatePass, trigger: "blur" }],
        checkPass: [{ validator: validatePass2, trigger: "blur" }],
      },
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // alert('submit!');
          fetch(`/api/loginServlet`, {
            method: "post",
            body: `uname=${this.ruleForm.pass}&upwd=${this.ruleForm.checkPass}`,
            headers: {
              "Content-Type": "application/x-www-form-urlencoded",
            },
          })
            .then((res) => res.json())
            .then((res) => {
              if (res.status == "0") {
                this.$message({
                  message: "恭喜你，登录成功！",
                  type: "success",
                });
                this.$router.push('/')
              } else {
                this.$message.error('登录失败，用户名或者密码错误！');
              }
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
  mounted() {
    fetch("/api/judgeLoginServlet", {
      method: "post",
    }).then(res => res.json())
    .then(res => {
      if(res.status == "0") {
        this.$router.push('/')
      }
    })
  },
};
</script>

<style scoped>
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100vw;
  height: 100vh;
}
.form {
  padding: 45px 0;
  text-align: center;
  /* border: 1px solid #ccc; */
  box-shadow: 0 0 10px #ccc;
  border-radius: 22px;
}
.title {
  margin: 22px;
}
</style>